﻿using System.Threading.Tasks.Dataflow;

namespace 营救公主
{
    internal class Program
    {
        public static int sceneId = 0;
        static void Main(string[] args)
        {
            Scene scene = new ();
            while (true)
            {
                switch (sceneId)
                {
                    case 0:
                        scene.start();
                        break;
                    case 1:
                        scene.play();
                        break;
                    case 2:
                        scene.over();
                        break;
                    default:
                        break;
                }
            }
        }
    }
#pragma warning disable CA1416
    class Scene
    {
        static int winWidth = 50, winHeight = 30;
        static int bufWidth = 50, bufHeight = 30;
        int playerX = bufWidth * 3 / 4, playerY = bufHeight * 3 / 4;
        int bossX = bufWidth / 2, bossY = bufHeight / 2;
        public Scene()
        {
            Console.SetWindowSize(winWidth, winHeight);
            Console.SetBufferSize(bufWidth, bufHeight);
            Console.CursorVisible = false;

        }
        public void start()
        {
            bool choice = true;
            Console.Clear();
            //Console.WriteLine("开始场景");
            write(true);
            while (Program.sceneId == 0) {
                switch (Console.ReadKey(true).Key)
                {
                    case ConsoleKey.W:
                    case ConsoleKey.UpArrow:
                        choice = true;
                        write(true);
                        break;
                    case ConsoleKey.S:
                    case ConsoleKey.DownArrow:
                        choice = false;
                        write(false);
                        break;
                    case ConsoleKey.Enter:
                        if (choice)
                            Program.sceneId = 1;
                        else
                            Environment.Exit(0);
                        break;
                    default:
                        break;
                }
            }
        }
        public void write(bool begin)
        {
            Console.Clear();
            Console.SetCursorPosition(bufWidth / 2 - 4, bufHeight / 4);
            if (begin)
                Console.ForegroundColor = ConsoleColor.Red;
            else
                Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("开始游戏");
            if (begin)
                Console.ForegroundColor = ConsoleColor.White;
            else
                Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(bufWidth / 2 - 4, bufHeight / 2);
            Console.WriteLine("结束游戏");
            Console.ForegroundColor = ConsoleColor.White;
        }
        public void play()
        {
            Console.Clear();
            //Console.WriteLine("游戏场景");
            writeBlock();
            writeBoss();
            writePlayer();
            while (Program.sceneId == 1)
                switch (Console.ReadKey(true).Key)
                {
                    case ConsoleKey.W:
                        if ((playerY - 1 == bossY) && (bossX - 1 <= playerX && playerX <= bossX + 1))
                            break;
                        erase();
                        if (playerY - 1 > 0) playerY -= 1;
                        writePlayer();
                        break;
                    case ConsoleKey.S:
                        if ((playerY + 1 == bossY) && (bossX - 1 <= playerX && playerX <= bossX + 1))
                            break;
                        erase();
                        if (playerY + 1 < bufHeight - 1) playerY += 1;
                        writePlayer();
                        break;
                    case ConsoleKey.A:
                        if ((playerY == bossY) && (bossX - 1 <= playerX - 2 && playerX - 2 <= bossX + 1))
                            break;
                        erase();
                        if (playerX - 2 > 1) playerX -= 2;
                        writePlayer();
                        break;
                    case ConsoleKey.D:
                        if ((playerY == bossY) && (bossX - 1 <= playerX + 2 && playerX + 2 <= bossX + 1))
                            break;
                        erase();
                        if (playerX + 2 < bufWidth - 3) playerX += 2; ;
                        writePlayer();
                        break;
                    case ConsoleKey.R:
                        if ((playerY == bossY && Math.Abs(bossX - playerX) <= 3) ||
                                (Math.Abs(playerY - bossY) <= 1 && Math.Abs(bossX - playerX) <= 1))
                        {
                            Console.SetCursorPosition(1, bufHeight * 7 / 8);
                            Console.WriteLine("打败Boss，营救公主成功！");
                            Program.sceneId = 2;
                        }
                        break;
                    default:
                        break;
                }
        }

        private void erase()
        {
            Console.SetCursorPosition(playerX, playerY);
            Console.Write("  ");
        }

        private void writePlayer()
        {
            String player = "▲";
            Console.SetCursorPosition(playerX, playerY);
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write(player);
        }

        private void writeBoss()
        {
            String boss = "★";
            Console.SetCursorPosition(bossX,bossY);
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.Write(boss);
        }

        public void writeBlock()
        {
            String block = "■";
            Console.ForegroundColor = ConsoleColor.Red;
            for (int i = 0; i < bufWidth - 1; i++)
            {
                Console.SetCursorPosition(i, 0);
                Console.Write(block);
                Console.SetCursorPosition(i, bufHeight - 1); 
                Console.Write(block);
            }
            for (int j = 1; j < bufHeight - 1; j++)
            {
                Console.SetCursorPosition(0, j);
                Console.Write(block);
                Console.SetCursorPosition(bufWidth - 2, j);
                Console.Write(block);
            }
        }
        public void over()
        {
            Console.Clear();
            Console.SetCursorPosition(bufWidth / 2 - 4, bufHeight / 4);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("解救成功!!");
            Console.ReadKey(true);
            Program.sceneId = 0;
        }

    }

       
}
